package week6;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

/**
 * @author The Setting sun
 * @create 2021-10-14 15:27
 */

public class Work5 {
    private JFrame frmV;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Work5 window = new Work5();
                    window.frmV.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Work5() {
        initialize();
    }

    private void initialize() {
        frmV = new JFrame();
        frmV.setTitle("\u6211\u7684\u8BB0\u4E8B\u672C V1.0");
        frmV.setBounds(100, 100, 450, 300);
        frmV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmV.getContentPane().setLayout(null);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(0, 0, 436, 221);
        frmV.getContentPane().add(textArea);

        JFileChooser jfc=new JFileChooser();

        /*打开按键的单击事件*/
        JButton btnOpen = new JButton("\u6253\u5F00");
        btnOpen.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                int select=jfc.showOpenDialog(frmV);
                if(select==JFileChooser.CANCEL_OPTION) {
                    return;
                }else if(select==JFileChooser.APPROVE_OPTION) {
                    File file=jfc.getSelectedFile();	//创建一个文件对象来获取所选中的文件

                    try {
                        FileReader fr=new FileReader(file);
                        BufferedReader br=new BufferedReader(fr);

                        String line;

                        while((line=br.readLine())!=null){
                            textArea.append(line+"\n");
                        }
                        br.close();
                        fr.close();

                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }


        });
        btnOpen.setBounds(22, 231, 97, 23);
        frmV.getContentPane().add(btnOpen);

        /*另存为按键的单击事件*/
        JButton btnSaveAs = new JButton("\u53E6\u5B58\u4E3A");
        btnSaveAs.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int select=jfc.showSaveDialog(frmV);

                if(select==JFileChooser.CANCEL_OPTION) {
                    return;
                }else if(select==JFileChooser.APPROVE_OPTION) {
                    File file=jfc.getSelectedFile();

                    try {
                        FileWriter fw=new FileWriter(file);
                        BufferedWriter bw=new BufferedWriter(fw);

                        bw.write(textArea.getText());

                        bw.close();
                        fw.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        });
        btnSaveAs.setBounds(317, 231, 97, 23);
        frmV.getContentPane().add(btnSaveAs);

        /*保存按键的单击事件*/
        JButton btnSave = new JButton("\u4FDD\u5B58");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                File file=jfc.getSelectedFile();

                try {
                    FileWriter fw=new FileWriter(file);
                    BufferedWriter bw=new BufferedWriter(fw);

                    bw.write(textArea.getText());

                    bw.close();
                    fw.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        btnSave.setBounds(176, 231, 97, 23);
        frmV.getContentPane().add(btnSave);
    }
}
